#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash pymol-v2.5.0.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

if [[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]]; then echo "请输入正确的构建路径"; exit 1; fi
if [[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]]; then echo "请输入正确的安装路径"; exit 1; fi

buildpath=$1
installpath=$2
if [[ "$1" =~ .*/$ ]]; then buildpath=${buildpath%/*}; fi
if [[ "$2" =~ .*/$ ]]; then installpath=${installpath%/*}; fi
if [[ "$buildpath" = "$installpath" ]]; then echo "构建路径和安装路径相同，请重新输入"; exit 1; fi

set -e
yum install -y wget
yum install -y kernel-devel cmake
yum install -y glew glew-devel
yum install -y libpng libpng-devel
yum install -y freetype freetype-devel
yum install -y libxml2 libxml2-devel
yum install -y mesa*
yum install -y freeglut*
yum install -y doxygen tkinter subversion

yum install -y gdm
yum install -y Xorg*
yum install -y xterm

wget -t 0 -c -P $buildpath https://github.com/schrodinger/pymol-open-source/archive/refs/tags/v2.5.0.tar.gz
mv $buildpath/v2.5.0.tar.gz $buildpath/pymol-open-source-2.5.0.tar.gz
tar -xvf $buildpath/pymol-open-source-2.5.0.tar.gz -C $buildpath && rm -rf $buildpath/pymol-open-source-2.5.0.tar.gz
cd $buildpath/pymol-open-source-2.5.0

export NETCDF=$installpath/netcdf
export LD_LIBRARY_PATH=$NETCDF/lib:$LD_LIBRARY_PATH
export LIBRARY_PATH=$NETCDF/lib:$LIBRARY_PATH
$installpath/python3/bin/python3 setup.py build install --prefix=$installpath/pymol --glut --use-openmp no

cat>"$installpath/pymol/pymol_modulefiles"<<EOF
#%Module1.0
conflict pymol
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set PYMOL \$pwd
setenv PYMOL \$PYMOL
prepend-path PATH \$PYMOL/bin
prepend-path LD_LIBRARY_PATH \$PYMOL/lib
EOF

